package com.lam.generator.service.impl;

import java.util.List;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.lam.common.utils.CollectionUtil;
import com.lam.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.lam.generator.mapper.GenCodeModuleMapper;
import com.lam.generator.entity.GenCodeModule;
import com.lam.generator.param.GenCodeModuleParam;
import com.lam.generator.service.GenCodeModuleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.common.components.easyexcel.imp.model.AbstractImportBody;
import com.lam.common.components.easyexcel.imp.model.ImportEntry;
import com.lam.common.components.easyexcel.imp.model.ImportWrapper;
import com.lam.common.components.easyexcel.imp.service.AbstractImportService;
import com.lam.common.constant.CommonConstant;
import com.lam.generator.entity.imp.GenCodeModuleImp;
import com.lam.common.validate.model.UniqueDesc;
import com.lam.common.validate.ValidateUtil;
import com.lam.common.validate.model.ValidOperType;
import com.lam.generator.common.constant.GeneratorConstant;
import com.lam.generator.common.util.GenUtils;

import java.text.MessageFormat;

/**
 * 代码模块Service业务层处理
 * 
 * @author admin
 * @date 2022-11-01
 */
@Service
public class GenCodeModuleServiceImpl extends AbstractImportService<GenCodeModuleImp, GenCodeModule> implements GenCodeModuleService {

    @Autowired
    private GenCodeModuleMapper genCodeModuleMapper;

    @Override
	public GenCodeModule findModule(String tableName) {
    	
    	GenCodeModuleParam param = new GenCodeModuleParam();
    	param.setStatus(CommonConstant.STATUS_NORMAL_1);
		List<GenCodeModule> list = selectList(param);
		String tablePrefix;
		for (GenCodeModule codeModule : list) {
			if(StringUtils.isNotBlank(codeModule.getTablePrefix())) {
				tablePrefix = GenUtils.matchTablePrefix(codeModule, tableName);
				if(StringUtils.isNotBlank(tablePrefix)) {
					return codeModule;
				}
			}
		}
		return CollectionUtil.getFirst(list);
	}
    
    /**
     * 查询代码模块
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public GenCodeModule selectById(String id) {

        return genCodeModuleMapper.selectById(id);
    }

    /**
     * 查询代码模块列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<GenCodeModule> selectList(GenCodeModuleParam param) {
    
        QueryWrapper<GenCodeModule> queryWrapper = new QueryWrapper<>();
        
        //关键字查询
        if(StringUtils.isNotBlank(param.getKeywords())) {
            queryWrapper.and(wr->{
                wr.or().like("project_module", param.getKeywords());
                wr.or().like("module", param.getKeywords());
                wr.or().like("module_short", param.getKeywords());
                wr.or().like("module_name", param.getKeywords());
                wr.or().like("package_name", param.getKeywords());
                wr.or().like("table_prefix", param.getKeywords());
            });
        }
        
        queryWrapper.like(StringUtils.isNotBlank(param.getProjectModuleKey()), "project_module", param.getProjectModuleKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getProjectModule()), "project_module", param.getProjectModule());
        queryWrapper.like(StringUtils.isNotBlank(param.getModuleKey()), "module", param.getModuleKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getModule()), "module", param.getModule());
        queryWrapper.like(StringUtils.isNotBlank(param.getModuleShortKey()), "module_short", param.getModuleShortKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getModuleShort()), "module_short", param.getModuleShort());
        queryWrapper.like(StringUtils.isNotBlank(param.getModuleNameKey()), "module_name", param.getModuleNameKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getModuleName()), "module_name", param.getModuleName());
        queryWrapper.like(StringUtils.isNotBlank(param.getPackageNameKey()), "package_name", param.getPackageNameKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getPackageName()), "package_name", param.getPackageName());
        queryWrapper.like(StringUtils.isNotBlank(param.getTablePrefixKey()), "table_prefix", param.getTablePrefixKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getTablePrefix()), "table_prefix", param.getTablePrefix());
        queryWrapper.eq(Objects.nonNull(param.getStatus()), "status", param.getStatus());

        return genCodeModuleMapper.selectList(queryWrapper);
    }

    /**
     * 新增代码模块
     * 
     * @param genCodeModule 
     * @return 
     */
    @Override
    @Transactional
    public int insert(GenCodeModule genCodeModule) {
    
        return genCodeModuleMapper.insert(genCodeModule);
    }

    /**
     * 修改代码模块
     * 
     * @param genCodeModule 
     * @return 
     */
    @Override
    @Transactional
    public int update(GenCodeModule genCodeModule) {
    
        return genCodeModuleMapper.updateById(genCodeModule);
    }

    /**
     * 删除代码模块信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    
        if(StringUtils.isBlank(idStr)) {
            return 0;
        }
        return genCodeModuleMapper.deleteBatchIds(StringUtils.str2List(idStr));
    }
    
    /**
     * 导入代码模块数据
     * @param wrapper 代码模块数据包装对象
     */
    @Override
    @Transactional
    public void importData(ImportWrapper<GenCodeModuleImp, GenCodeModule> wrapper) {
        
        UniqueDesc uniqueDesc = ValidateUtil.selectUniqueDesc(GeneratorConstant.UNIQUE_CODE_MODULE);
        importData(wrapper, new AbstractImportBody<GenCodeModuleImp, GenCodeModule>() {
            @Override
            public GenCodeModule selectRecord(ImportEntry<GenCodeModuleImp, GenCodeModule> entry) {
                return ValidateUtil.selectUniqueData(entry.getData(), ValidOperType.ADD, uniqueDesc, GenCodeModule.class);
            }
            @Override
			public int insertData(GenCodeModule data) {
				return insert(data);
			}
			@Override
			public int updateData(GenCodeModule data) {
				return update(data);
			}
            @Override
            public String notUniqueMsg(ImportEntry<GenCodeModuleImp, GenCodeModule> entry) {
                return MessageFormat.format("模块【{0}】已存在", entry.getData().getModule());
            }
        });
    }

}
